import cv2 as cv
import numpy as np


def resize_img(image, height=1800):
    h, w = image.shape[:2]
    pro = height / h
    size = (int(w * pro), int(height))
    img = cv.resize(image, size)
    return img


path = '/Users/wanggh/Desktop/c.jpeg'

src = cv.imread(path)
src = resize_img(src)
# 灰度
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# 高斯平滑
gray = cv.GaussianBlur(gray, (3, 3), sigmaX=5, sigmaY=5, borderType=cv.BORDER_DEFAULT)
# 边缘检测
gray = cv.Canny(gray, 25, 50, apertureSize=3, L2gradient=False)
kernel = np.ones((3, 3), np.uint8)
gray = cv.dilate(gray, kernel, iterations=0)

lines = cv.HoughLines(gray, 1, np.pi / 180, 200)

for line in lines:
    rho, theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a * rho
    y0 = b * rho
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * (a))
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * (a))
    cv.line(src, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv.imshow("test", src)
cv.waitKey(0)
cv.destroyAllWindows()
